package com.bdqn.controller;

import com.bdqn.pojo.Role;
import com.bdqn.pojo.User;
import com.bdqn.service.RoleService;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public String list(Model model, String usrName, Long roleId, Long pageIndex) {
        // 当前要查询的页码，如果是null则设置为默认查第1页
        pageIndex = pageIndex == null ? 1 : pageIndex;

        // 查询所有角色
        List<Role> roles = roleService.list();

//        // 分页查询用户列表
//        Page<User> page = new Page<>();
//        page.setCurrent(pageIndex);
//        page = userService.findPage(usrName, roleId, page);
//
//        // 设置数据到作用域
//        model.addAttribute("roles", roles);
//        model.addAttribute("userPager", page);

        return "user/list";
    }


    @GetMapping("/add")
    public String add(Model model) {
        // 查询所有角色
        List<Role> roles = roleService.list();
        model.addAttribute("roles", roles);
        return "user/add";
    }

    @ResponseBody
    @GetMapping("/check")
    public Object check(@RequestParam String usrName) {
        Map resultMap = new HashMap<>();
        User user = userService.selectUserName(usrName);
        if (user != null) {
            resultMap.put("result", "true");
        }
        return resultMap;
    }

    @PostMapping("/save")
    public String save(User user) {
        if (user.getUsrFlag() == null) {
            user.setUsrFlag(0);
        }
        int add = userService.saveUser(user);
        if (add > 0) {
            return "redirect:/user/list";
        } else {
            return "user/add";
        }
    }


    @GetMapping("/edit")
    public String edit(@RequestParam("usrId") Long usrId, Model model) {
        // 查询所有角色
        List<Role> roles = roleService.list();
        model.addAttribute("roles", roles);

        // 根据id查询用户
        User user = userService.getUserById(usrId);
        model.addAttribute("user", user);

        return "user/edit";
    }


    @PostMapping("/saveUpd")
    public String upd(User user) {
        if (user.getUsrFlag() == null) {
            user.setUsrFlag(0);
        }
        int upd = userService.updateUser(user);
        if (upd > 0) {
            return "redirect:/user/list";
        } else {
            return "user/edit";
        }
    }

    @ResponseBody
    @PostMapping("/del/{usrId}")
    public Object del(@PathVariable long usrId) {
        int del = userService.deleteUser(usrId);
        Map<Object, Object> resultMap = new HashMap<>();
        if (del > 0) {
            resultMap.put("result", "true");
        }
        return resultMap;
    }
}
